function FaultAlarmMan(){
	var refreshTime = 0;
	var fdata = [];
	var div = $('#bottomMarquee');
	var gif = '/resources/img/daily/fault.gif';
	var gwidth = 35;
	var gheight = 35;
	var popIdBefore = "faultAlarmPop";//+equipment_id+equipment
	//点图层：weatherLayer
	var pointIdBefore = "faultAlarmPoint";//+equipment_id+equipment
	
	this.show = function(){
		initData();
		$.getJSON("/rest/getRefreshTime?time="+new Date().getTime(),function(result){
			refreshTime=result['refreshTime'];//获取故障滚动条（地图下方的跑马灯）的刷新时长
			window.setInterval(function() {
				if(div.css('display') == 'block'){
					initData();
				}
			},refreshTime);
		});
		return this;
	};
	this.refresh = function(){
		clearAll(true);
		return this;
	};
	this.clickPoint = function(key){
		if(!removePop(key)){
			drawPop(key);
		}
	};
	this.selectFault = function(key){
		if(isFeatureShow(key)){
			removeFeatures(key);
		}else{
			if(showFeatures(key)){
				var name = getSearchName(key);
				var id = getSearchId(key);
				searchSelectObj(name,true,false,id);
			}else{
				layer.msg('故障定位失败',{time:2000});
			}
		}
	};
	function initData(){
		$.getJSON('/rest/fault/getAlarmFault',function(result){
			if(result && result.length>0){
				div.css('display','block');
				fdata = result;
				for(var i in fdata){
					if(!fdata[i].lon){
						fdata[i].lon = 0;
					}
					if(!fdata[i].lat){
						fdata[i].lat = 0;
					}
				}
				drawBottomMarquee(fdata);
			}else{
				div.css('display','none');
			}
		});
	}
	function getKey(f){
		var s ;
		if(f.mapSearchName){
			s = f.mapSearchName + ','+f.mapSearchId;
		}else{
			s = f.equipment+','+f['equipment_id'];
			if(f.etype != '线路' && f.stationName){
				s = f.stationName+','+f.stationId;
			}
		}
		
		return f.lon+','+f.lat+','+s;
	}
	function getSearchName(key){
		return key.split(',')[2];
	}
	function getSearchId(key){
		return key.split(',')[3];
	}
	function isFeatureShow(key){
		var feature = weatherLayer.getFeatureById(pointIdBefore+key);
		if(feature){
			return true;
		}else{
			return false;
		}
	}
	function changeIconToMarker(icon){
		icon.addClass('show-in-map');
		icon.css('color','red');
	}
	function drawPop(key) {
		var value = getFaultsByEidAndEquipment(key);
		if(value.length == 0){
			return;
		}
		var pop = new Popup(value[0].lon,value[0].lat);
		pop.setTitle(getSearchName(key));
		pop.setId(popIdBefore+key);
		for(var i in value){
			var v = value[i];
			var t = DateUtil.dateTime(v.faultTime);
			var clicklistener = 
				'faultAlarmMan.openPopRowPop("'+
				t+'","'+v['equipment_id']+'")';
		
			pop.setRowContent(FaultAlarmMan.getFaultPopRowContent(value[i]))
			.setRowIconClickListener(clicklistener)
			.setRowSelfClickListener(clicklistener)
			.setRowSelfHoverPopupContent(FaultAlarmMan.getFaultProcessContent(value[i]));
		}
		pop.setAutoSize(true);
		pop.showCloseIcon(true);
		pop.show();
	}
	function removeFeatures(key){
		var icon = getIcon(key);
		var f = weatherLayer.getFeatureById(pointIdBefore+key);
		if(f){
			weatherLayer.removeFeatures([f]);
		}
		removePop(key);
		clearSearchObj(getSearchId(key));
		clearSearchObj(getSearchName(key));
		changeIconToList(icon);
	}
	function getIcon(key){
		return $("i[data-key='"+key+"']");
	}
	function removePop(key){
		var pops = map.popups;
		for(var i=0;i<pops.length;i++){
			if(pops[i].id == popIdBefore+key){
				map.removePopup(pops[i]);
				return true;
			}
		}
		return false;
	}
	function showFeatures(key){
		var icon = getIcon(key);
		var faults = getFaultsByEidAndEquipment(key);
		if(faults.length>0 && faults[0].lon && faults[0].lat){
			drawPop(key);
			drawPoint(key);
			changeIconToMarker(icon);
			return true;
		}
		return false;
	}
	function getFaultsByEidAndEquipment(key){
		var fs = [];
		for(var i in fdata){
			var k = getKey(fdata[i]);
			if(k == key){
				fs.push(fdata[i]);
			}
		}
		return fs;
	}
	function changeIconToList(icon){
		icon.removeClass('show-in-map');
		icon.css('color','black');
	}
	function drawBottomMarquee(data){
		div.html('');
		var time = new Date();
		div.append('<span style="margin-right:20px;cursor:pointer;" class="badge bg-red" onclick="faultAlarmMan.refresh();" title="'+time.getHours()+':'+time.getMinutes()+'">New</span>');
		for(var i in data){
			var d = data[i];
			var map;
			var key = getKey(d);
			if(isFeatureShow(key)){
				map = '<i style="margin-right:10px;color:red;" title="切换显示状态" data-key="'+key+'" class="fa fa-map-marker show-in-map"></i>';
			}else{
				map = '<i style="margin-right:10px;" title="切换显示状态" data-key="'+key+'" class="fa fa-map-marker "></i>';
			}
			var t = new Date(d['faultTime'].time);
			var faultTime = (t.getMonth()+1) + '月' + t.getDate() + '日  ' + t.getHours() + '时' + t.getMinutes() + '分';
			var a = '<a href="#" onclick="faultAlarmMan.selectFault(\''+key+'\');" style="color:black" >'+map+getSpan(d.equipment)+(d.startPole?getSpan(d.startPole+'号杆塔'):'')+'<span style="color:red;margin-right:5px;">'+ faultTime + '</span>'
			+ getSpan(d['faultType']+"故障")+ getSpan((d.faultPhase?d.faultPhase:'--')+'相');
			div.append(a);
			div.append('<span style="margin-right:100px;"></span>');
		}
		div.append('<span style="margin-right:200px;"></span>');
		div.liMarquee();
		
	}
	this.hide = function(){
		div.css('display','none');
		return this;
	};
	function getSpan(text){
		return '<span style="margin-right:10px;">'+text+'</span>';
	}
	this.openPopRowPop = function(faultTime,lineId) {
		var url = "/ui/map/protectActSvg/protectActSvg.xhtml?lineId=" + lineId+"&faultTime="+faultTime;
		MyModal.showModal(url);
	};
	function drawPoint(key){
		var faults = getFaultsByEidAndEquipment(key);
		if(faults.length == 0){
			return;
		}
		var fault = faults[0];
		var point = new SuperMap.Geometry.Point(fault.lon,
				fault.lat);
		var gifPoint = new SuperMap.Feature.Vector(point, null, {
			externalGraphic : gif,
			graphicWidth : gwidth,
			graphicHeight : gheight,
			graphicOpacity : 1,
		});
		gifPoint.data.type = 'faultAlarm';
		gifPoint.data.value = key;
		gifPoint.id = pointIdBefore+key;
		
		weatherLayer.addFeatures([gifPoint]);
		weatherLayer.setVisibility(true);
		selectFeature.activate();
	}
}
FaultAlarmMan.getFaultProcessContent = function(fault) {
	var content = '<div style="width:400px;">';
	content += fault.description ? fault.description+'<br/>' : '';
	content = content.replace(/\r\n/g, '<br/>');
	content = content.replace(/\n/g, '<br/>');
	content = content.replace(/\s/g, '&nbsp;');
	if(fault.thunderInfo || fault.iceInfo ){
		content += '<hr style="margin:5px;"//>';
		content += fault.thunderInfo ? fault.thunderInfo+'<br/>' : '';
		content += fault.iceInfo ? fault.iceInfo+'<br/>' : '';
	}
	content += "</div>";
	return content;
}
FaultAlarmMan.getFaultPopRowContent = function(fault) {
	var t = new Date(fault['faultTime'].time);
	var faultTime = (t.getMonth()+1) + '月' + t.getDate() + '日  ' + t.getHours() + '时' + t.getMinutes() + '分';
	var c = '<span style="color:red;">'+ faultTime + '</span> '
	+ '<span style="margin-right:5px;">' + fault['faultType']+ '</span>';
	if(fault['thunderInfo'].indexOf('有落雷')>-1){
		c += '<span style="margin-left:5px;color:#f39c12" class="fa fa-bolt"></span>';
	}
	if(fault['iceInfo'].indexOf('发生覆冰')>-1){
		c += '<span style="margin-left:5px;color:#337ab7" class="fa fa-snowflake-o"></span>';
	}
	return c;
}